import { get, post, post_json } from '../../http/axios'
export default {
  namespaced: true,
  state: {
    orders: [],
    // 全部订单数据
    orderList: [],
    // 查询订单
    orderList2: []
  },
  mutations: {
    refreshOrder(state, orders) {
      state.orders = orders
    },
    GET_AllOrderData(state, allOrderList){
      state.orderList = allOrderList
    },
    SET_ORDERLIST2(state,data){
          if(state.orderList2.length == 0){
            state.orderList2 =JSON.parse(JSON.stringify(data));
          }
    },
    FIND_ORDER_BY_ID(state,search){
      
      state.orderList = state.orderList2.filter((order)=>{
        return order.id == search
      })
    
    }
  },
  actions: {
    // 提交订单
    async SubmitOrder({ commit }, data) {
      // 调用接口完后保存
      let response = await post_json('/app/order/submitOrder', data)
      // console.log(response)
      if (response.data.message == '操作成功') {
        // 清空购物车
         commit('shopcar/clearShopcar', null, { root: true })
      } else {
        
      }
      return response
    },
    // 获取所有订单数据
    async findAllOrderDataVuex({ commit }, data){
      let res = await get('/order/pageQuery', data)
     /// console.log(res.data.data.list)
      commit('GET_AllOrderData', res.data.data.list)
      commit('SET_ORDERLIST2', res.data.data.list)
    },



  }
}